﻿using Microsoft.AspNetCore.Mvc;
using Novel.Web.Models;
using Novel.Web.Models.Entity;
using Novel.Web.Models.Novel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using XjjXmm.FrameWork.ToolKit;
using XjjXmm.Infrastructure.Repository;
using XjjXmm.Infrastructure.Swagger;
using XjjXmm.Spider.Entity;

namespace Novel.Web.Controllers
{
    [HiddenApi]
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly MenuService _menuService;
        private readonly NovelService _novelService;
        private readonly NovelFacade novelFacade;
        private readonly SuggestService suggestService;

        public HomeController(ILogger<HomeController> logger,
            MenuService menuService, NovelService novelService,
            NovelFacade novelFacade, SuggestService suggestService)
        {
            _logger = logger;
            _menuService = menuService;
            _novelService = novelService;
            this.novelFacade = novelFacade;
            this.suggestService = suggestService;
        }

        [HttpGet]
        public async Task<IActionResult> Index(int page = 1)
        {
            var res = await novelFacade.FetchHome(page);
            
            return View(res);
        }

        // [Description("玄幻小说")]
        [HttpGet]
        public async Task<IActionResult> List(string category, int page = 1)
        {
           
            var res = await novelFacade.FetchSub(category, page);

            return View("List",res);
        }

        [HttpGet]
        [Route("/chapter/{id}")]
        public async Task<IActionResult> Chapter(long id)
        {
            var res = await novelFacade.FetchChapters(id);
            return View(res);
        }

        [HttpGet]
        [Route("/content/{nid}/{cid}")]
        public async Task<IActionResult> Content(long nid, long cid)
        {
            var res = await novelFacade.FetchContent(nid, cid);

            return View("Detail", res);
        }

        [HttpPost]
        [Route("/suggest/view")]
        public async Task<IActionResult> Suggest()
        {
            return View("suggest");
        }

        [HttpPost]
        [Route("/suggest/add")]
        public async Task<IActionResult> AddSuggest(SuggestEntity suggestEntity)
        {
            var res = await suggestService.Add(suggestEntity);

            return View("suggest");
        }


        [HttpGet]
        public IActionResult Privacy()
        {
            return View();
        }

        [HttpGet]
        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }
}